﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KingRoad.EPM.DeviceEntity.Model;
using KingRoad.EPM.Web.Areas.Device.Models;
using KingRoad.EPM.Entity;
using DotNet.Service;
using DotNet.Model.MyEntity;
using KingRoad.EPM.DeviceBLL.UIRules;
using System.Dynamic;

namespace KingRoad.EPM.Web.Areas.Device.Controllers
{
    public class MeterController : BaseController
    {

       
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
           int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = 1;
            //每页多少条
            ViewBag.numPerPage = 20;
           // var rlist = BllMeterUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Id", out totalRecord, "", "desc", "");
            var rlist = BllMeterUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Id", out totalRecord, "", "desc", "");
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View(new MeterModel { list = rlist });
        }

       

        /// <summary>
        /// 列表(分页提交)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SplitPage()
        {
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = Convert.ToInt32(Request.Form["pageNum"]);
            //每页多少条
            ViewBag.numPerPage = Convert.ToInt32(Request.Form["numPerPage"]);
            ViewBag.iput_type = Request.Form["iput_type"];
            ViewBag.iput_code = Request.Form["iput_code"];
        //    ViewBag.PlacId = Request.Form["input_placId"];
          //  Base_Organize oInfo = ServiceManager.Instance.OrganizeService.GetOrganizeEntityById(ViewBag.PlacId);
        //    ViewBag.danwei = oInfo.FullName;
            string sql = " 1=1 ";
            if (ViewBag.iput_type != "")
                sql += " and Model like '%" + ViewBag.iput_type + "%' ";
            if (ViewBag.iput_code != "")
                sql += " and Code like '%" + ViewBag.iput_code + "%' ";
            //if (ViewBag.PlacId != "")
            //    sql += " and PlacId= " + ViewBag.PlacId + " ";
            var rlist = BllMeterUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Id", out totalRecord, sql, "desc", "");
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View("Index", new MeterModel { list = rlist });
        }

        /// <summary>
        /// 绑定列表
        /// </summary>
        /// <returns></returns>
        public ActionResult IndexQuery(string roadId)
        {
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = 1;
            //传车道ID
            ViewBag.roadId = roadId;
            //每页多少条
            ViewBag.numPerPage = 20;
            // var rlist = BllMeterUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Id", out totalRecord, "", "desc", "");
            var rlist = BllMeterUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Id", out totalRecord, " UseState='入库' ", "desc", "");
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View(new MeterModel { list = rlist });
        }
        /// <summary>
        /// 绑定列表(分页提交)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult IndexQuerySplitPage()
        {
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = Convert.ToInt32(Request.Form["pageNum"]);
            //每页多少条
            ViewBag.numPerPage = Convert.ToInt32(Request.Form["numPerPage"]);
            ViewBag.iput_type = Request.Form["iput_type"];
            ViewBag.iput_code = Request.Form["iput_code"];
            //    ViewBag.PlacId = Request.Form["input_placId"];
            //  Base_Organize oInfo = ServiceManager.Instance.OrganizeService.GetOrganizeEntityById(ViewBag.PlacId);
            //    ViewBag.danwei = oInfo.FullName;
            string sql = " 1=1 and UseState='入库' ";
            if (ViewBag.iput_type != "")
                sql += " and Model like '%" + ViewBag.iput_type + "%' ";
            if (ViewBag.iput_code != "")
                sql += " and Code like '%" + ViewBag.iput_code + "%' ";
            //if (ViewBag.PlacId != "")
            //    sql += " and PlacId= " + ViewBag.PlacId + " ";
            var rlist = BllMeterUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Id", out totalRecord, sql, "desc", "");
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View("IndexQuery", new MeterModel { list = rlist });
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public ActionResult IndexStatus()
        {
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = 1;
            //每页多少条
            ViewBag.numPerPage = 20;
            // var rlist = BllMeterUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Id", out totalRecord, "", "desc", "");
            var rlist = BllMeterUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Id", out totalRecord, "roadid<>0", "desc", "");
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View(new MeterModel { list = rlist });
        }



        /// <summary>
        /// 列表(分页提交)
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SplitPageStatus()
        {
            int totalRecord = 0;
            //当前页码
            ViewBag.pageNum = Convert.ToInt32(Request.Form["pageNum"]);
            //每页多少条
            ViewBag.numPerPage = Convert.ToInt32(Request.Form["numPerPage"]);
            ViewBag.iput_type = Request.Form["iput_type"];
            ViewBag.iput_code = Request.Form["iput_code"];

            ViewBag.sel_time = Request.Form["sel_time"];
            
            string sql = " roadid<>0 ";
            if (ViewBag.iput_type != "")
                sql += " and Model like '%" + ViewBag.iput_type + "%' ";
            if (ViewBag.iput_code != "")
                sql += " and Code like '%" + ViewBag.iput_code + "%' ";
           
            var rlist = BllMeterUIRule.GetEntityListByContionRe(ViewBag.pageNum, ViewBag.numPerPage, "Id", out totalRecord, sql, "desc", "");
            //总条数
            ViewBag.totalcount = totalRecord;
            //总页数
            ViewBag.pagenumshown = totalRecord % ViewBag.numPerPage == 0 ? totalRecord / ViewBag.numPerPage : totalRecord / ViewBag.numPerPage + 1;
            return View("IndexStatus", new MeterModel { list = rlist });
        }

        //绑定仪表
        public JsonResult MeterBinding(int id, int roadId)
        {
            bool isScuess = true;
            try
            {
                var vm = BllMeterUIRule.GetEntityById(id);
                vm.RoadId = roadId;
                vm.UseState = "使用";
                var bm = BllMeterUIRule.UpdateEntity(vm);
            }
            catch (Exception ex)
            {
                isScuess = false;
            }
            dynamic jsonReturn = new ExpandoObject();
            //绑定成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "绑定成功",
                    navTabId = "Device.Roadway.List",
                    rel = "",
                    callbackType = "closeCurrent",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "绑定失败",
                };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        //解除绑定

        //绑定仪表
        public JsonResult UNMeterBinding(int meterid)
        {
            bool isScuess = true;
            try
            {
                var vm = BllMeterUIRule.GetEntityById(meterid);
                if (vm!=null)
                {
                    vm.RoadId = 0;
                    vm.UseState = "入库";
                    var bm = BllMeterUIRule.UpdateEntity(vm);    
                }
                
            }
            catch (Exception ex)
            {
                isScuess = false;
            }
            dynamic jsonReturn = new ExpandoObject();
            //绑定成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "解绑成功",
                    navTabId = "Device.Roadway.List",
                    rel = "",
                    callbackType = "closeCurrent",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "解绑失败",
                };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

          /// <summary>
        /// add
        /// </summary>
        /// <returns></returns>
        public ActionResult MeterAdd()
        {
            return View();
         }
        public JsonResult MeterAddHandle(MeterModel vm)
        {
            bool isScuess = true;
            try
            {
                vm.info.ReportTime = DateTime.Now;
                vm.info.UseState = "入库";
                BllMeterUIRule.InsertEntity(vm.info);
            }
            catch (Exception ex)
            {
                isScuess = false;
            }

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "添加成功",
                    navTabId = "Device.Meter.Index",
                    rel = "",
                    callbackType = "closeCurrent",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "添加失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        public ActionResult MeterEdit(int id,string target)
        {
            BllMeterInfo rInfo = BllMeterUIRule.GetEntityById(id);
            ViewBag.OperationType = target;
            //Base_Organize oInfo = ServiceManager.Instance.OrganizeService.GetOrganizeEntityById(rInfo.PlacId.ToString());
            //ViewBag.placId = rInfo.PlacId;
            //ViewBag.placeName = oInfo.FullName;
            return View(new MeterModel { info = rInfo });
        }

        public JsonResult MeterEditHandle(MeterModel vm)
        {
            bool isScuess = true;
            try
            {
                BllMeterUIRule.UpdateEntity(vm.info);
            }
            catch (Exception ex)
            {
                isScuess = false;
            }

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "修改成功",
                    navTabId = "Device.Meter.Index",
                    rel = "",
                    callbackType = "closeCurrent",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "修改失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }


        public JsonResult MeterDelete(string id)
        {
            bool isScuess = true;
            try
            {
                var st = BllMeterUIRule.DeleteEntity(id);
            }
            catch (Exception ex)
            {
                isScuess = false;
            }
            dynamic jsonReturn = new ExpandoObject();
            //删除成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "删除成功",
                    navTabId = "ybxx_list",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "删除失败",
                };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

    }
}
